package com.tfjybj.itoo.exam.provider.controller;

import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.model.StaffModel;
import com.tfjybj.itoo.exam.provider.service.StaffService;
import com.tfjybj.itoo.teach.FacadeApi.StaffApi;
import com.tfjybj.itoo.teach.entity.StaffEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * StaffController
 * staff表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 10:35:56
 */
@Api(tags = {"staff表接口"})
@RequestMapping(value = "/staff")
@RestController
public class StaffController {

    @Resource
    private StaffService staffService;

    @Reference(check = false)
    private StaffApi staffApi;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model StaffModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody StaffModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        StaffEntity staffEntity = new StaffEntity();
        BeanUtils.copyProperties(model, staffEntity);
        staffApi.save(staffEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        staffApi.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        staffApi.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model StaffModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id修改staff")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody StaffModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        StaffEntity staffEntity = new StaffEntity();
        BeanUtils.copyProperties(model, staffEntity);
        staffApi.updateById(staffEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Staff
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        StaffEntity staffEntity = staffApi.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffEntity);
    }

    /**
     * 分页查询所有Staff
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "分页查询所有Staff")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StaffEntity> staffs = staffApi.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffs);
    }

    /**
     * 根据教师姓名模糊查询staff
     *
     * @param name     教师姓名
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的staff
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据教师姓名模糊查询staff", notes = "分页根据教师姓名模糊查询staff")
    @GetMapping(value = "queryStaffByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryStaffByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                           @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                           @ApiParam(value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StaffModel> staffList = staffService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据登陆人id和创建考试的开始时间,筛选监考老师(带有模糊查询)
     *
     * @param teacherId   登陆老师id
     * @param startTime   考试开始时间
     * @param endTime     考试结束时间
     * @param teacherName 筛选框中的值
     * @return 返回筛选结果集
     * @author maying
     * @since 2018-12-8 15:23:25
     */
    @ApiOperation(value = "根据登陆人id和创建考试的开始时间,筛选监考老师")
    @GetMapping(value = "/queryInvigilator/{teacherId}/{startTime}/{endTime}")
    public ItooResult queryInvigilator(@RequestParam(required = false, defaultValue = "") String teacherName,
                                       @PathVariable(required = false) String teacherId,
                                       @PathVariable(required = false) String startTime,
                                       @PathVariable String endTime) {
        List<StaffModel> staffModelList = staffService.queryInvigilator(teacherName, teacherId, startTime, endTime);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功!", staffModelList);
    }

}    
